package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.service.ICommodityEvaluationService;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.shopping.models.bo.shop.ShopBrandRelationBO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.service.shop.IShopCollectService;
import com.ztgf.shopping.models.vo.shop.ShopBrandRelationVO;
import com.ztgf.shopping.models.vo.shop.ShopInfoBrandVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.shop.ShopBrandRelationMapper;
import com.ztgf.shopping.models.service.shop.IShopBrandRelationService;
import com.ztgf.shopping.models.bo.shop.ShopBrandRelationSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopBrandRelationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺品牌关系表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 14:43:46
 */
@DubboService
public class ShopBrandRelationServiceImpl extends ServiceImpl<ShopBrandRelationMapper, ShopBrandRelationDTO> implements IShopBrandRelationService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private IShopCollectService iShopCollectService;

    @DubboReference
    private ICommodityInfoService iCommodityInfoService;

    @DubboReference
    private ICommodityEvaluationService iCommodityEvaluationService;

    @Override
    public boolean add(ShopBrandRelationSaveBO saveBO) {
        ShopBrandRelationDTO dto = new ShopBrandRelationDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public Page<ShopInfoBrandVO> queryFranchiseShopPageList(Page<ShopInfoBrandVO> page, ShopBrandRelationBO bo) {
        Page<ShopInfoDTO> dtopage = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<ShopInfoDTO> shopInfoDTOPageList = baseMapper.queryFranchiseShopPageList(dtopage, bo);
        List<ShopInfoBrandVO> shopInfoBrandVOList = queryShopVo(shopInfoDTOPageList);
        page.setTotal(dtopage.getTotal());
        page.setSize(dtopage.getSize());
        page.setRecords(shopInfoBrandVOList);
        return page;
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        ShopBrandRelationDTO dto = this.selectById(id);
        if (dto == null) {
            return false;
        }
        dto.setStatus(status);
        //若为删除或过期，下架所有相关商品
        if (status == 1 || status == 2) {
            iCommodityInfoService.updateBatchPublishByShopBrand(dto.getShopId(), dto.getBrandId());
        }
        return this.updateById(dto);
    }

    @Override
    public Page<ShopInfoBrandVO> querySpecialtyShopPageList(Page<ShopInfoBrandVO> page, ShopBrandRelationBO bo) {
        Page<ShopInfoDTO> dtopage = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<ShopInfoDTO> shopInfoDTOPageList = baseMapper.querySpecialtyShopPageList(dtopage, bo);
        List<ShopInfoBrandVO> shopInfoBrandVOList = queryShopVo(shopInfoDTOPageList);
        page.setRecords(shopInfoBrandVOList);
        return page;
    }

    @Override
    public Integer getShopCount(Long brandId) {
        Wrapper<ShopBrandRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("brand_id", brandId).eq("status", 0);
        return this.selectCount(wrapper);
    }

    @Override
    public Page<ShopInfoBrandVO> queryShopPageList(Page<ShopInfoBrandVO> page, ShopBrandRelationBO bo) {
        Page<ShopInfoDTO> dtopage = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<ShopInfoDTO> shopInfoDTOPageList = baseMapper.queryShopPageList(dtopage, bo);
        List<ShopInfoBrandVO> shopInfoBrandVOList = queryShopVo(shopInfoDTOPageList);
        page.setRecords(shopInfoBrandVOList);
        return page;
    }

    @Override
    public List<ShopBrandRelationVO> queryVOList(Long id) {
        return baseMapper.queryVOList(id);
    }

    @Override
    public Page<ShopInfoBrandVO> queryShopListByBrandId(ShopBrandRelationBO bo) {
        Page<ShopInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<ShopInfoDTO> list = baseMapper.queryShopListByBrandId(page, bo);
        Page<ShopInfoBrandVO> pageA = new Page<>(bo.getPageNo(), bo.getPageSize());
        if (CustomizeUtil.listIsNull(list)) {
            return pageA;
        }
        List<ShopInfoBrandVO> listA = this.queryShopVo(list);
        pageA.setTotal(page.getTotal());
        pageA.setSize(page.getSize());
        pageA.setRecords(listA);
        return pageA;
    }

    @Override
    public ShopBrandRelationDTO getDTO(Long shopId, Long brankId) {

        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("shop_id", shopId);
        wrapper.eq("brand_id", brankId);
        wrapper.eq("status", 0);
        return this.selectOne(wrapper);
    }

    private List<ShopInfoBrandVO> queryShopVo(List<ShopInfoDTO> shopInfoDTOS) {
        ArrayList<ShopInfoBrandVO> shopInfoBrandVOList = new ArrayList<ShopInfoBrandVO>();
        for (ShopInfoDTO shopInfoDTO : shopInfoDTOS) {
            ShopInfoBrandVO shopInfoBrandVO = new ShopInfoBrandVO();
            BeanUtils.copyProperties(shopInfoDTO, shopInfoBrandVO);
            Long shopId = shopInfoDTO.getId();
            //根据shop查询出被收藏数
            shopInfoBrandVO.setCollectNum(iShopCollectService.getBeCollectShopCount(shopId));

            //根据shop查询出商品总数
            shopInfoBrandVO.setCommodityNum(iCommodityInfoService.getCommodityNumByShopId(shopId));

            //根据shop查询出是否有新品
            shopInfoBrandVO.setIsNew(iCommodityInfoService.getShopCommodityIsNew(shopId));

            //根据shop查询出好评率
            shopInfoBrandVO.setPositiveRate(iCommodityEvaluationService.getShopPraise(shopId));
            shopInfoBrandVO.setId(shopId);
            shopInfoBrandVOList.add(shopInfoBrandVO);
        }
        return shopInfoBrandVOList;
    }
}
